package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.appcompat.widget.AppCompatTextView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.MyExpertsAdapter;
import com.digitalchina.lxnsy.api.BusinessExpert;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyExpertVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.widget.MyListView;
import com.digitalchina.lxnsy.xml.LocationXML;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的专家 Created by zhy on 2018/1/12 0012.
 */

public class MyExpertActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.add_experts_image)
    ImageView addExpertsImage;
    @Bind(R.id.focus)
    ImageView focus;
    @Bind(R.id.add_experts_btn)
    AppCompatTextView addExpertsBtn;
    @Bind(R.id.more)
    AppCompatTextView more;
    @Bind(R.id.listView)
    MyListView listView;  // 专家团队

    private static final String LIMIT = "6";
    private List<MyExpertVO> expertList = new ArrayList<>();

    private MyExpertsAdapter mAdapter;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_expert);
        setTitle("我的专家");
//        PersonalInfoXML.setGoMyExpert(this,"isGo");
        focus.setFocusable(true);
        focus.setFocusableInTouchMode(true);
        focus.requestFocus();

    }

    @Override
    public void initMember() {
        BusinessExpert.orgAppList(this, "", LocationXML.getCity(this), String.valueOf(1), LIMIT, mHandler);
    }

    @Override
    public void addListener() {
        addExpertsImage.setOnClickListener(this);
        addExpertsBtn.setOnClickListener(this);
        more.setOnClickListener(this);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map<String, Object> map = new HashMap<>();
                map.put("MyExpertVO", mAdapter.getItem(position));
                GotoUtil.gotoActivity(MyExpertActivity.this, AddMyExpertsActivity.class, true, "map", map);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);


                switch (msg.what) {
                    case MSG.MSG_MYEXPERTS_LIST_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_MYEXPERTS_LIST_FIELD:
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        expertList = FastJsonUtil.getListBean(object.toString(), "orgList", MyExpertVO.class);

        if (expertList == null)
            return;

        this.mAdapter = new MyExpertsAdapter(this, expertList);
        this.listView.setAdapter(this.mAdapter);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.more:
                GotoUtil.gotoActivity(this, MyExpertListActivity.class, true);
                break;
            case R.id.add_experts_btn:
                GotoUtil.gotoActivity(this, AddMyExpertsActivity.class,true, "fromMyExpert", true);
                break;
            case R.id.add_experts_image:
                GotoUtil.gotoActivity(this, AddMyExpertsActivity.class, true,"fromMyExpert", true);
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_my_expert_list)
    public void onViewClicked() {
        GotoUtil.gotoActivity(this, OperateExpertListActivity.class, true);
    }
}
